<template>
  <div>
    <my-breadcrumb :items="['消息管理', '敏感词管理']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">添加敏感词</el-button>
    </div>
    <el-table :data="sensitive" @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        sortable="custom"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="敏感词"
        prop="content">
      </el-table-column>
      <el-table-column
        label="是否严重"
        prop="isserious">
        <template slot-scope="scope">
          {{ scope.row.isserious === '1' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column
        label="添加时间"
        prop="updatetime">
      </el-table-column>
      <el-table-column
        label="操作"
        prop="id">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '新增敏感词' : '信息修改'" :visible.sync="showAdd" width="300px">
      <el-form :model="sen" label-width="70px" ref="addForm">
        <el-form-item label="敏感词" prop="content">
          <el-input v-model="sen.content"/>
        </el-form-item>
        <el-form-item label="是否严重" prop="isserious">
          <el-select v-model="sen.isserious">
            <el-option value="1" label="是"/>
            <el-option value="0" label="否"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deleteRow} from '../../utils/getList'
export default {
  name: 'sensitive',
  data () {
    return {
      searchItems: [
        {
          label: '敏感词',
          type: 'input',
          key: 'content'
        },
        {
          label: '是否严重',
          key: 'isserious',
          type: 'select',
          options: [{id: '1', name: '是'}, {id: '0', name: '否'}]
        }
      ], // 检索结构
      sensitive: [],
      sen: {
        content: '',
        isserious: '0'
      },
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      isAdd: true,
      showAdd: false,
      updateRow: null,
      lifting: '1'
    }
  },
  methods: {
    // /Sensitive/index
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Sensitive/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.sensitive = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(this.sen).forEach(key => {
          this.sen[key] = row[key]
        })
      })
    },
    toSubmit () {
      let params = {}
      let url = '/Sensitive/add'
      Object.keys(this.sen).forEach(key => {
        params[key] = this.sen[key]
      })
      if (!this.isAdd) {
        url = '/Sensitive/edit'
        params.id = this.updateRow.id
      }
      this.axios.post(url, this.qs.stringify(params))
        .then(res => {
          if (res.data.status === 1) {
            this.$message.success(res.data.msg)
            this.showAdd = false
            this.getList()
          }
        })
    },
    toDelete (id) {
      deleteRow({url: '/Sensitive/delete', id}, res => {
        if (res.data.status === '1' || res.data.status === 1) {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    },

    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = '2'
      } else {
        this.lifting = '1'
      }
      this.getList()
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>

</style>
